package cn.jingzhuan.element.demo.view.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutCompat
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import cn.jingzhuan.element.demo.view.BaseBindingFragment
import cn.jingzhuan.element.demo.view.activity.AudioDetailActivity
import cn.jingzhuan.element.demo.view.adapter.AdapterWrapper
import cn.jingzhuan.element.demo.view.adapter.BaseBindingRecyclerAdapter
import cn.jingzhuan.element.demo.view.adapter.BindingRecyclerViewHolder
import cn.jingzhuan.element.demo.view.adapter.SwipeToLoadHelper
import cn.jingzhuan.element.demo.view.model.AudioItem
import cn.jingzhuan.element.demo.view.util.UnitParseUtils
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.FragmentAlbumAudioBinding
import cn.jingzhuan.element.element.databinding.ItemAudioInfoBinding


class AlbumAudioFragment : BaseBindingFragment<FragmentAlbumAudioBinding>() {

    private var mLoadMoreHelper: SwipeToLoadHelper? = null
    var audioList = mutableListOf<AudioItem>()
    var order = true // 默认正序
    var index = 1


    fun loadData() {
//        Thread.sleep(1000)
        audioList.add(AudioItem(index++,"The Winter","https://img3.doubanio.com/view/subject/m/public/s4433001.jpg","http://mr3.doubanio.com/97b7c0f84fbcae0f40b28475f95a53ac/1/fm/song/p48145_128k.mp4",1234))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s1796854.jpg","http://mr1.doubanio.com/18e606084eb0974484ee076fc2fe7823/0/fm/song/p163753_128k.mp4",12345))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s3673272.jpg","http://mr3.doubanio.com/5d75448bab425d3a8bed73919e6b3aa0/0/fm/song/p1387895_128k.mp4",123456))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        audioList.add(AudioItem(index++,"第$index 期声音","https://img3.doubanio.com/view/subject/m/public/s6331905.jpg","http://mr3.doubanio.com/2589748e106769ecdf9b5b292891d643/1/fm/song/p1651219_128k.mp4",12345612))
        if (audioList.size < 10) {
            mLoadMoreHelper?.setSwipeToLoadEnabled(false)
        }
        binding.recyclerView.adapter.notifyDataSetChanged()
    }

    override fun getLayoutId(): Int = R.layout.fragment_album_audio

    override fun onDataBind(savedInstanceState: Bundle?, binding: FragmentAlbumAudioBinding) {
        val layoutManager = LinearLayoutManager(context, LinearLayoutCompat.VERTICAL, false)
        binding.recyclerView.layoutManager = layoutManager

        binding.recyclerView.adapter = AdapterWrapper(object : BaseBindingRecyclerAdapter<AudioItem>(context!!, audioList){

            override fun getItemLayoutId(viewType: Int): Int  = R.layout.item_audio_info

            override fun bindData(holder: BindingRecyclerViewHolder, position: Int, item: AudioItem) {
                var audioInfoBinding = holder.binding as ItemAudioInfoBinding
                audioInfoBinding.orderNum.text = "${item.id}"
                audioInfoBinding.audioTitle.text = item.title
                audioInfoBinding.albumUpdateTime.text = "今日"
                audioInfoBinding.audioListen.text = UnitParseUtils.parseNumber(item.listen,1)
                audioInfoBinding.audioTime.text = "5:01"
                audioInfoBinding.audioListened.visibility = View.GONE

                audioInfoBinding.root.setOnClickListener{
                    var intent = Intent(context, AudioDetailActivity::class.java)
                    intent.putExtra("image",item.image)
                    startActivity(intent)
                }
            }
        })

        mLoadMoreHelper = SwipeToLoadHelper(binding.recyclerView, binding.recyclerView.adapter as AdapterWrapper<AudioItem>)

        loadData()

        initListener()

    }

    private fun initListener() {
        // 播放全部
        binding.audioPlayAll .setOnClickListener {
            Toast.makeText(context,"播放全部！",Toast.LENGTH_SHORT).show()
        }

        // 切换排序
        binding.audioChangeOrder.setOnClickListener {

        }

        // 刷新事件
        binding.refreshLayout.setOnRefreshListener {
            onRefresh()
        }

        // 加载更多监听事件
        mLoadMoreHelper?.setLoadMoreListener(object : SwipeToLoadHelper.LoadMoreListener{
            override fun onLoad() {
                loadData()
                onLoadMoreComplete()
            }
        })

    }


    fun onRefresh() {
//        binding.refreshLayout.isEnabled = false
        audioList.clear()
        loadData()
        binding.refreshLayout.isRefreshing = false
//        onRefreshComplete()
    }

    // 数据刷新完成后调用
    private fun onRefreshComplete() {
        mLoadMoreHelper?.setLoadMoreFinish()
        binding.refreshLayout.isRefreshing = false
        binding.refreshLayout.isEnabled = true
    }

    // 数据加载完成后调用
    private fun onLoadMoreComplete() {
        binding.refreshLayout.isEnabled = true
        mLoadMoreHelper?.setLoadMoreFinish()
        binding.recyclerView.adapter.notifyDataSetChanged()
    }

    // 数据加载出错后调用
    fun onInitLoadFailed() {
        binding.refreshLayout.visibility = View.GONE
    }

}